<template>
  <el-dialog v-dialog-drag append-to-body :close-on-click-modal="false" width="1000px" :visible.sync="visible"
    :before-close="beforeClose" :title="title">
    <div class="wrapper">
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="计划名称" prop="title">
          <el-input v-model="form.title" :disabled="operateType == 'view'" placeholder="请输入计划名称" clearable />
        </el-form-item>
        <el-form-item label="计划内容" prop="remark">
          <el-input :disabled="operateType == 'view'" type="textarea" :rows="3"
            v-model="form.remark" :maxlength="-1" :show-word-limit="false" :autosize="{ minRows: 2, maxRows: 4 }">
          </el-input>
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" size="mini" @click="submit" v-if="operateType != 'view'">确定</el-button>
      <el-button size="mini" @click="beforeClose">取消</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { addData, updateData, getRoles } from "../api";
export default {
  name: "AddDialog",
  props: {
    visible: Boolean,
    title: {
      type: String,
      default: "质量检查计划详情"
    },
    data: {
      type: Object,
      default: () => { }
    },
    operateType: {
      type: String,
      default: "view"
    }
  },
  data() {
    return {
      projectId: this.$store.state.app.selectData,
      form: {
        title: "",
        remark: '',
      },
      // 表单校验
      rules: {
        title: [
          { required: true, message: "清单类型不能为空", trigger: "change" },
        ],
      },
    };
  },
  created() {
    this.initData();
  },
  methods: {
    initData() {
      if (this.operateType == 'view') {
        Object.keys(this.rules).forEach(item => {
          delete this.rules[item]
        })
      }
      if (this.operateType == 'edit' || this.operateType == 'view') {
        Object.keys(this.data).forEach(key => {
          this.$set(this.form, key, this.data[key])
        })
      }
    },
    beforeClose() {
      this.$emit('update:visible', false);
    },
    async submit() {
      this.$refs.form.validate(async valid => {
        if (!valid) {
          return;
        }
        const fn = this.form.id ? updateData : addData;
        const res = await fn({ ...this.form, projectId: this.projectId })
        if (res.code == 200) {
          this.$emit("refresh")
          this.beforeClose()
        }
      })
    },
  },
};
</script>
<style lang="scss" scoped>
.wrapper {
  max-height: 500px;
  overflow-y: auto;
  overflow-x: hidden;

  .el-select {
    width: 100%;
  }

  .el-date-editor {
    width: 100%;
  }
}
</style>
